package view.aptc.baiviet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputFile;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewObject;
import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

import org.apache.myfaces.trinidad.model.UploadedFile;

import oracle.jbo.RowSet;
import oracle.jbo.ViewCriteriaRow;

import org.apache.myfaces.trinidad.event.SelectionEvent;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

import view.common.Utils;


public class baivietbean {
    private RichPopup newPopupVN;
    private RichPopup newPopupEN;
    private RichPopup viewPopupVN;
    private RichPopup viewPopupEN;
    private RichPopup newPopupDeleteVN;
    private RichPopup newPopupDeleteEN;
    private RichInputFile inputFileVN;
    private RichInputFile inputFileEN;
    private UploadedFile _fileVN;
    private UploadedFile _fileEN;
    private static int operationType = 0;
    private RichCommandButton buttonDownload;

    private static String soSelectedValueVN;
    private SelectItem[] soElementsVN = null;
    private String chuyenmucSelectedValueVN;
    private SelectItem[] chuyenmucElementsVN = null;

    private static String soSelectedValueEN;
    private SelectItem[] soElementsEN = null;
    private String chuyenmucSelectedValueEN;
    private SelectItem[] chuyenmucElementsEN = null;

    private RichSelectOneChoice soSelectChoiceVN;
    private RichSelectOneChoice soSelectChoiceEN;
    private RichSelectOneChoice chuyenmucSelectChoiceVN;
    private RichSelectOneChoice chuyenmucSelectChoiceEN;

    private String tieudeTimkiemVN;
    private String tieudeTimkiemEN;
    private int pageNumberVN;
    private int pageNumberEN;
    private static final int RANGE_SIZE = 15;
    private String resetview;
    private boolean unZipFile = false; //false: Keep File, true: Un ZipFile
    private String fileDeleted;
    private UIComponent iterVN;
    private UIComponent iterEN;
    private static boolean hasFile = false;
    private boolean fileExisted = false;
    private static ArrayList<String> currentFiles = new ArrayList<String>();
    private String filedinhkemVn;
    private String filedinhkemEn;
    private String tieudeVN;
    private String tieudeEN;

    private static List<String> validExt =
        Arrays.asList("doc", "docx", "xls", "xlsx", "pdf", "tif", "jpg",
                      "jpeg", "gif", "png", "bmp", "zip", "rar");

    public baivietbean() {
        JUCtrlRangeBinding baivietViewVN = this.getBaivietViewVN();
        baivietViewVN.getIteratorBinding().getNavigatableRowIterator().scrollRange(0);

        JUCtrlRangeBinding baivietViewEN = this.getBaivietViewEN();
        baivietViewEN.getIteratorBinding().getNavigatableRowIterator().scrollRange(0);
    }

    public String getResetview() {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            DCIteratorBinding baivietIteratorVN =
                Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
            baivietIteratorVN.getViewObject().getViewCriteriaManager().clearViewCriterias();
            baivietIteratorVN.executeQuery();

            DCIteratorBinding baivietIteratorEN =
                Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
            baivietIteratorEN.getViewObject().getViewCriteriaManager().clearViewCriterias();
            baivietIteratorEN.executeQuery();
            //AdfFacesContext.getCurrentInstance().addPartialTarget(tblVN);
        }
        return resetview;
    }

    public void createVN(ActionEvent actionEvent) {
        Utils.iteratorRefresh("AptcBaivietView_VN1Iterator");
        Utils.execute("CreateInsertVN");
        Utils.showPopup(newPopupVN);
        soSelectedValueVN = null;
        soSelectChoiceVN.setValue(null);
        chuyenmucSelectChoiceVN.setValue(null);
    }

    public void createEN(ActionEvent actionEvent) {
        Utils.iteratorRefresh("AptcBaivietView_EN1Iterator");
        Utils.execute("CreateInsertEN");
        Utils.showPopup(newPopupEN);
        soSelectedValueEN = null;
        soSelectChoiceEN.setValue(null);
        chuyenmucSelectChoiceEN.setValue(null);
    }

    public void updateVN(ActionEvent actionEvent) {
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
        if (baivietIterator.getCurrentRow() == null) {
            String messageText = "Bạn chưa chọn bài viết nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            soSelectedValueVN =
                    baivietIterator.getCurrentRow().getAttribute("AptcSoId").toString();
            chuyenmucSelectedValueVN =
                    baivietIterator.getCurrentRow().getAttribute("ChuyenmucId").toString();
            soSelectChoiceVN.setValue(soSelectedValueVN);
            chuyenmucSelectChoiceVN.setValue(chuyenmucSelectedValueVN);

            if (baivietIterator.getCurrentRow().getAttribute("FileId") !=
                null) {
                hasFile = true;
                DCIteratorBinding fileIterator =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                ViewObject vo = fileIterator.getViewObject();
                ViewCriteria vc = vo.createViewCriteria();
                ViewCriteriaRow cr = vc.createViewCriteriaRow();
                String varStr =
                    "IN (" + baivietIterator.getCurrentRow().getAttribute("FileId") +
                    ")";
                cr.setAttribute("Id", varStr);
                vc.addElement(cr);
                vo.getViewCriteriaManager().clearViewCriterias();
                vo.applyViewCriteria(vc);
                vo.executeQuery();
                String str[] =
                    baivietIterator.getCurrentRow().getAttribute("FileId").toString().split(",");
                if (str.length > 0) {
                    for (int i = 0; i < str.length; i++) {
                        currentFiles.add(str[i]);
                    }
                }
            } else {
                hasFile = false;
            }
            this.operationType = 1;
            Utils.showPopup(newPopupVN);
        }
    }

    public void updateEN(ActionEvent actionEvent) {
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
        if (baivietIterator.getCurrentRow() == null) {
            String messageText = "Bạn chưa chọn bài viết nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            soSelectedValueEN =
                    baivietIterator.getCurrentRow().getAttribute("AptcSoId").toString();
            chuyenmucSelectedValueEN =
                    baivietIterator.getCurrentRow().getAttribute("ChuyenmucId").toString();
            soSelectChoiceEN.setValue(soSelectedValueEN);
            chuyenmucSelectChoiceEN.setValue(chuyenmucSelectedValueEN);

            if (baivietIterator.getCurrentRow().getAttribute("FileId") !=
                null) {
                hasFile = true;
                DCIteratorBinding fileIterator =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                ViewObject vo = fileIterator.getViewObject();
                ViewCriteria vc = vo.createViewCriteria();
                ViewCriteriaRow cr = vc.createViewCriteriaRow();
                String varStr =
                    "IN (" + baivietIterator.getCurrentRow().getAttribute("FileId") +
                    ")";
                cr.setAttribute("Id", varStr);
                vc.addElement(cr);
                vo.getViewCriteriaManager().clearViewCriterias();
                vo.applyViewCriteria(vc);
                vo.executeQuery();
                String str[] =
                    baivietIterator.getCurrentRow().getAttribute("FileId").toString().split(",");
                if (str.length > 0) {
                    for (int i = 0; i < str.length; i++) {
                        currentFiles.add(str[i]);
                    }
                }
            } else {
                hasFile = false;
            }
            this.operationType = 1;
            Utils.showPopup(newPopupEN);
        }
    }

    public void valueChangedVN(ValueChangeEvent valueChangeEvent) {
        soSelectedValueVN = valueChangeEvent.getNewValue().toString();
        getChuyenmucVN(soSelectedValueVN);
    }

    public void valueChangedEN(ValueChangeEvent valueChangeEvent) {
        soSelectedValueEN = valueChangeEvent.getNewValue().toString();
        getChuyenmucEN(soSelectedValueEN);
    }

    public void chuyenmucVNValueChanged(ValueChangeEvent valueChangeEvent) {
        chuyenmucSelectedValueVN = valueChangeEvent.getNewValue().toString();
    }

    public void chuyenmucENValueChanged(ValueChangeEvent valueChangeEvent) {
        chuyenmucSelectedValueEN = valueChangeEvent.getNewValue().toString();
    }

    private void getChuyenmucVN(String soid) {
        if (soid.isEmpty() || soid == null) {
            soid = "0";
        }
        DCIteratorBinding soChuyenmucIterator =
            Utils.getIteratorBinding("AptcSoChuyenmucView_VN1Iterator");
        ViewObject vo = soChuyenmucIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "=" + soid;
        cr.setAttribute("AptcSoId", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
        int length = vo.getAllRowsInRange().length;
        chuyenmucElementsVN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = vo.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("ChuyenmucId").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            chuyenmucElementsVN[i] = item;
        }
    }

    private void getChuyenmucEN(String soid) {
        if (soid.isEmpty() || soid == null) {
            soid = "0";
        }
        DCIteratorBinding soChuyenmucIterator =
            Utils.getIteratorBinding("AptcSoChuyenmucView_EN1Iterator");
        ViewObject vo = soChuyenmucIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "=" + soid;
        cr.setAttribute("AptcSoId", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
        int length = vo.getAllRowsInRange().length;
        chuyenmucElementsEN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = vo.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("ChuyenmucId").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            chuyenmucElementsEN[i] = item;
        }
    }

    public void commitVN(DialogEvent dialogEvent) {
        Boolean validated = true;
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
        ArrayList<String> fileList = new ArrayList<String>();
        if (_fileVN != null) {
            if (Utils.checkFileExtension(_fileVN.getFilename(), validExt)) {
                String ext =
                    _fileVN.getFilename().substring(_fileVN.getFilename().lastIndexOf("."),
                                                    _fileVN.getFilename().length());

                String dirRoot =
                    Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"); //Thu muc file goc
                String dirFile =
                    Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"); //duong dan tuong doi thu muc file
                File dirFilePath =
                    new File(Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator")); //duong dan tuong doi thu muc file
                File dir =
                    new File(dirRoot, dirFile); //Duong dan tuyet doi thu muc file
                Object fileId = null;
                DCIteratorBinding fileIterator1 =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                if (this.unZipFile) {
                    try {
                        if (ext.equals(".zip")) {
                            ZipInputStream zipInputStream =
                                new ZipInputStream(_fileVN.getInputStream());
                            ZipEntry zipEntry = zipInputStream.getNextEntry();
                            while (zipEntry != null) {
                                Utils.execute("HtFileInsert");
                                fileId =
                                        fileIterator1.getCurrentRow().getAttribute("Id").toString();
                                if (zipEntry.isDirectory() == false) {
                                    File fileName =
                                        fileName = new File(fileId + "_" +
                                                            zipEntry.getName());
                                    File fileEx =
                                        new File(dir, fileName.toString());
                                    File filePath =
                                        new File(dirFile, fileName.toString());
                                    if (fileEx.getParentFile().exists() ==
                                        false) {
                                        fileEx.getParentFile().mkdirs();
                                    }
                                    FileOutputStream fileExOut =
                                        new FileOutputStream(fileEx);
                                    byte[] buffer = new byte[1024];
                                    int len = 0;
                                    while ((len =
                                            zipInputStream.read(buffer)) !=
                                           -1) {
                                        fileExOut.write(buffer, 0, len);
                                    }
                                    fileIterator1.getCurrentRow().setAttribute("TenFile",
                                                                               fileName.toString());
                                    fileIterator1.getCurrentRow().setAttribute("DuongDan",
                                                                               filePath.toString().replace("\\",
                                                                                                           "/"));
                                    fileIterator1.getCurrentRow().setAttribute("MaUngDung",
                                                                               Utils.ENUM_MA_UNG_DUNG.APTC.getMaUngDung());
                                    fileList.add(fileId.toString());
                                    fileExOut.flush();
                                    fileExOut.close();
                                }
                                zipEntry = zipInputStream.getNextEntry();
                            }
                            zipInputStream.close();
                        } else {
                            validated = false;
                            _fileVN = null;
                            inputFileVN.resetValue();
                            inputFileVN.setValue(null);
                            inputFileVN.setChanged(false);
                            inputFileVN.setSubmittedValue(null);
                            inputFileVN.setValid(false);
                            AdfFacesContext.getCurrentInstance().addPartialTarget(inputFileVN);
                            Utils.showMessage(Utils.MSG_ERROR,
                                              "File nén không hợp lệ! Hệ thống chỉ hỗ trợ giải nén file dạng *.zip.");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (fileList.size() > 0) {
                        String fileUploadedId = "";
                        for (int i = 0; i <= fileList.size() - 1; i++) {
                            fileUploadedId += fileList.get(i) + ",";
                        }
                        fileUploadedId =
                                fileUploadedId.substring(0, fileUploadedId.lastIndexOf(","));
                        baivietIterator.getCurrentRow().setAttribute("FileId",
                                                                     fileUploadedId);
                    }
                } else {
                    Utils.execute("HtFileInsert");
                    fileId =
                            fileIterator1.getCurrentRow().getAttribute("Id").toString();
                    if (Utils.fileUpload(this.getFileVN(), dir,
                                         fileId.toString())) {
                        Utils.processUpload(this.getFileVN(), dirFilePath,
                                            "HtFileView1Iterator",
                                            "AptcBaivietView_VN1Iterator",
                                            this.operationType,
                                            Utils.ENUM_MA_UNG_DUNG.APTC,
                                            fileId.toString());
                    }
                }
            } else {
                validated = false;
                _fileVN = null;
                inputFileVN.resetValue();
                inputFileVN.setValue(null);
                inputFileVN.setChanged(false);
                inputFileVN.setSubmittedValue(null);
                inputFileVN.setValid(false);
                AdfFacesContext.getCurrentInstance().addPartialTarget(inputFileVN);
                Utils.showMessage(Utils.MSG_ERROR,
                                  "Tập tin đính kèm chỉ chấp nhận các định dạng doc, docx, xls, xlsx, pdf, tif, jpg, jpeg, gif, png, bmp.");
            }
        } else {
            if (this.operationType == 1) {
                String fileUploadedId = "";
                if (currentFiles.size() > 0) {
                    for (int i = 0; i < currentFiles.size(); i++) {
                        fileUploadedId += currentFiles.get(i) + ",";
                    }
                    fileUploadedId =
                            fileUploadedId.substring(0, fileUploadedId.lastIndexOf(","));
                }
                baivietIterator.getCurrentRow().setAttribute("FileId",
                                                             fileUploadedId);
            }
        }

        if (tieudeVN.equals(baivietIterator.getCurrentRow().getAttribute("Tieude")) ==
            false) {
            if (Utils.checkExisted(tieudeVN, "Tieude",
                                   "AptcBaivietValidate1Iterator", null)) {
                validated = false;
                Utils.showMessage(Utils.MSG_ERROR,
                                  "Tiêu đề bạn vừa nhập đã tồn tại trong hệ thống.");
            }
        }

        if (validated) {
            String baivietId =
                baivietIterator.getCurrentRow().getAttribute("Id").toString();
            baivietIterator.getCurrentRow().setAttribute("Tieude", tieudeVN);
            baivietIterator.getCurrentRow().setAttribute("AptcSoId",
                                                         soSelectedValueVN);
            baivietIterator.getCurrentRow().setAttribute("ChuyenmucId",
                                                         chuyenmucSelectedValueVN);
            if (this.operationType == 0) {
                baivietIterator.getCurrentRow().setAttribute("NguoiNhap",
                                                             Utils.getCurrentUser());
                baivietIterator.getCurrentRow().setAttribute("NgayNhap",
                                                             Utils.getCurrentDate());
            }
            Utils.execute("Commit");
            Utils.iteratorRefresh("AptcBaivietView_VN1Iterator");
            Utils.iteratorRefresh("AptcBaivietView_EN1Iterator");
            int prePage =
                getBaivietViewVN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
            getBaivietViewVN().getIteratorBinding().getNavigatableRowIterator().scrollRange(RANGE_SIZE *
                                                                                            (pageNumberVN -
                                                                                             prePage));
            baivietIterator.setCurrentRowWithKeyValue(baivietId);
            Utils.iteratorRefresh("HtFileView1Iterator");
        }
        this.operationType = 0;
        hasFile = false;
        currentFiles.clear();
        this.unZipFile = false;
    }

    public void commitEN(DialogEvent dialogEvent) {
        Boolean validated = true;
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
        ArrayList<String> fileList = new ArrayList<String>();
        if (_fileEN != null) {
            if (Utils.checkFileExtension(_fileEN.getFilename(), validExt)) {
                String dirRoot =
                    Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"); //Thu muc file goc
                String dirFile =
                    Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"); //duong dan tuong doi thu muc file
                File dirFilePath =
                    new File(Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator")); //duong dan tuong doi thu muc file
                File dir =
                    new File(dirRoot, dirFile); //Duong dan tuyet doi thu muc file
                Object fileId = null;
                DCIteratorBinding fileIterator1 =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                if (this.unZipFile) {
                    try {
                        String ext =
                            _fileEN.getFilename().substring(_fileEN.getFilename().lastIndexOf("."),
                                                            _fileEN.getFilename().length());
                        if (ext.equals(".zip")) {
                            ZipInputStream zipInputStream =
                                new ZipInputStream(_fileEN.getInputStream());
                            ZipEntry zipEntry = zipInputStream.getNextEntry();
                            while (zipEntry != null) {
                                Utils.execute("HtFileInsert");
                                fileId =
                                        fileIterator1.getCurrentRow().getAttribute("Id").toString();
                                if (zipEntry.isDirectory() == false) {
                                    File fileName =
                                        fileName = new File(fileId + "_" +
                                                            zipEntry.getName());
                                    File fileEx =
                                        new File(dir, fileName.toString());
                                    File filePath =
                                        new File(dirFile, fileName.toString());
                                    if (fileEx.getParentFile().exists() ==
                                        false) {
                                        fileEx.getParentFile().mkdirs();
                                    }
                                    FileOutputStream fileExOut =
                                        new FileOutputStream(fileEx);
                                    byte[] buffer = new byte[1024];
                                    int len = 0;
                                    while ((len =
                                            zipInputStream.read(buffer)) !=
                                           -1) {
                                        fileExOut.write(buffer, 0, len);
                                    }
                                    fileIterator1.getCurrentRow().setAttribute("TenFile",
                                                                               fileName.toString());
                                    fileIterator1.getCurrentRow().setAttribute("DuongDan",
                                                                               filePath.toString().replace("\\",
                                                                                                           "/"));
                                    fileIterator1.getCurrentRow().setAttribute("MaUngDung",
                                                                               Utils.ENUM_MA_UNG_DUNG.APTC.getMaUngDung());
                                    fileList.add(fileId.toString());
                                    fileExOut.flush();
                                    fileExOut.close();
                                }
                                zipEntry = zipInputStream.getNextEntry();
                            }
                            zipInputStream.close();
                        } else {
                            validated = false;
                            _fileEN = null;
                            inputFileEN.resetValue();
                            inputFileEN.setValue(null);
                            inputFileEN.setChanged(false);
                            inputFileEN.setSubmittedValue(null);
                            inputFileEN.setValid(false);
                            AdfFacesContext.getCurrentInstance().addPartialTarget(inputFileEN);
                            Utils.showMessage(Utils.MSG_ERROR,
                                              "File nén không hợp lệ! Hệ thống chỉ hỗ trợ giải nén file dạng *.zip.");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (fileList.size() > 0) {
                        String fileUploadedId = "";
                        for (int i = 0; i <= fileList.size() - 1; i++) {
                            fileUploadedId += fileList.get(i) + ",";
                        }
                        fileUploadedId =
                                fileUploadedId.substring(0, fileUploadedId.lastIndexOf(","));
                        baivietIterator.getCurrentRow().setAttribute("FileId",
                                                                     fileUploadedId);
                    }
                } else {
                    Utils.execute("HtFileInsert");
                    fileId =
                            fileIterator1.getCurrentRow().getAttribute("Id").toString();
                    if (Utils.fileUpload(this.getFileEN(), dir,
                                         fileId.toString())) {
                        Utils.processUpload(this.getFileEN(), dirFilePath,
                                            "HtFileView1Iterator",
                                            "AptcBaivietView_EN1Iterator",
                                            this.operationType,
                                            Utils.ENUM_MA_UNG_DUNG.APTC,
                                            fileId.toString());
                    }
                }
            } else {
                validated = false;
                _fileEN = null;
                inputFileEN.resetValue();
                inputFileEN.setValue(null);
                inputFileEN.setChanged(false);
                inputFileEN.setSubmittedValue(null);
                inputFileEN.setValid(false);
                AdfFacesContext.getCurrentInstance().addPartialTarget(inputFileEN);
                Utils.showMessage(Utils.MSG_ERROR,
                                  "Tập tin đính kèm chỉ chấp nhận các định dạng doc, docx, xls, xlsx, pdf, tif, jpg, jpeg, gif, png, bmp.");
            }
        } else {
            if (this.operationType == 1) {
                String fileUploadedId = "";
                if (currentFiles.size() > 0) {
                    for (int i = 0; i < currentFiles.size(); i++) {
                        fileUploadedId += currentFiles.get(i) + ",";
                    }
                    fileUploadedId =
                            fileUploadedId.substring(0, fileUploadedId.lastIndexOf(","));
                }
                baivietIterator.getCurrentRow().setAttribute("FileId",
                                                             fileUploadedId);
            }
        }

        if (tieudeEN.equals(baivietIterator.getCurrentRow().getAttribute("Tieude")) ==
            false) {
            if (Utils.checkExisted(tieudeEN, "Tieude",
                                   "AptcBaivietValidate1Iterator",null)) {
                validated = false;
                Utils.showMessage(Utils.MSG_ERROR,
                                  "Tiêu đề bạn vừa nhập đã tồn tại trong hệ thống.");
            }
        }
        if (validated) {
            String baivietId =
                baivietIterator.getCurrentRow().getAttribute("Id").toString();
            baivietIterator.getCurrentRow().setAttribute("Tieude", tieudeEN);
            baivietIterator.getCurrentRow().setAttribute("AptcSoId",
                                                         soSelectedValueEN);
            baivietIterator.getCurrentRow().setAttribute("ChuyenmucId",
                                                         chuyenmucSelectedValueEN);
            if (this.operationType != 1) {
                baivietIterator.getCurrentRow().setAttribute("NguoiNhap",
                                                             Utils.getCurrentUser());
                baivietIterator.getCurrentRow().setAttribute("NgayNhap",
                                                             Utils.getCurrentDate());
            }
            Utils.execute("Commit");
            Utils.iteratorRefresh("AptcBaivietView_EN1Iterator");
            Utils.iteratorRefresh("AptcBaivietView_VN1Iterator");
            int prePage =
                getBaivietViewEN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
            getBaivietViewEN().getIteratorBinding().getNavigatableRowIterator().scrollRange(RANGE_SIZE *
                                                                                            (pageNumberEN -
                                                                                             prePage));
            baivietIterator.setCurrentRowWithKeyValue(baivietId);
            Utils.iteratorRefresh("HtFileView1Iterator");
        }
        this.operationType = 0;
        hasFile = false;
        currentFiles.clear();
        this.unZipFile = false;
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        if (this.operationType == 0) {
            Utils.execute("Rollback");
        }
        if (operationType == 1) {
            Utils.iteratorRefresh("HtFileView1Iterator");
            this.unZipFile = false;
            hasFile = false;
            currentFiles.clear();
        }
        this.operationType = 0;
    }

    public void deleteVN(DialogEvent dialogEvent) {
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
        String baivietId =
            baivietIterator.getCurrentRow().getAttribute("Id").toString();
        ViewObject vo = baivietIterator.getViewObject();
        vo.setAccessMode(RowSet.SCROLLABLE);
        baivietIterator.setCurrentRowWithKeyValue(baivietId);
        Object cFile = baivietIterator.getCurrentRow().getAttribute("FileId");
        if (cFile != null) {
            String str[] = cFile.toString().split(",");
            if (str.length > 0) {
                for (int i = 0; i < str.length; i++) {
                    currentFiles.add(str[i]);
                }
            }
            DCIteratorBinding fileIterator =
                Utils.getIteratorBinding("HtFileView1Iterator");
            for (int i = 0; i < currentFiles.size(); i++) {
                fileIterator.setCurrentRowWithKeyValue(currentFiles.get(i));
                fileIterator.removeCurrentRow();
            }
        }
        Utils.execute("DeleteVN");
        Utils.execute("Commit");
        hasFile = false;
        operationType = 0;
        currentFiles.clear();
        vo.setAccessMode(RowSet.RANGE_PAGING);
        //Utils.iteratorRefresh("AptcBaivietView_VN1Iterator");
    }

    public void deleteEN(DialogEvent dialogEvent) {
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
        String baivietId =
            baivietIterator.getCurrentRow().getAttribute("Id").toString();
        ViewObject vo = baivietIterator.getViewObject();
        vo.setAccessMode(RowSet.SCROLLABLE);
        baivietIterator.setCurrentRowWithKeyValue(baivietId);
        Object cFile = baivietIterator.getCurrentRow().getAttribute("FileId");
        if (cFile != null) {
            String str[] = cFile.toString().split(",");
            if (str.length > 0) {
                for (int i = 0; i < str.length; i++) {
                    currentFiles.add(str[i]);
                }
            }
            DCIteratorBinding fileIterator =
                Utils.getIteratorBinding("HtFileView1Iterator");
            for (int i = 0; i < currentFiles.size(); i++) {
                fileIterator.setCurrentRowWithKeyValue(currentFiles.get(i));
                fileIterator.removeCurrentRow();
            }
        }
        Utils.execute("DeleteEN");
        Utils.execute("Commit");
        hasFile = false;
        operationType = 0;
        currentFiles.clear();
        vo.setAccessMode(RowSet.RANGE_PAGING);
        //Utils.iteratorRefresh("AptcBaivietView_EN1Iterator");
    }

    public void setNewPopupVN(RichPopup newPopupVN) {
        this.newPopupVN = newPopupVN;
    }

    public RichPopup getNewPopupVN() {
        return newPopupVN;
    }

    public void setNewPopupEN(RichPopup newPopupEN) {
        this.newPopupEN = newPopupEN;
    }

    public RichPopup getNewPopupEN() {
        return newPopupEN;
    }

    public void setFileVN(UploadedFile _fileVN) {
        this._fileVN = _fileVN;
    }

    public UploadedFile getFileVN() {
        return _fileVN;
    }

    public void setFileEN(UploadedFile _fileEN) {
        this._fileEN = _fileEN;
    }

    public UploadedFile getFileEN() {
        return _fileEN;
    }

    public static void setOperationType(int operationType) {
        baivietbean.operationType = operationType;
    }

    public static int getOperationType() {
        return operationType;
    }

    public void deleteFileVN(ActionEvent actionEvent) {
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        fileIterator.setCurrentRowWithKeyValue(fileDeleted);
        fileIterator.removeCurrentRow();
        for (int i = 0; i < currentFiles.size(); i++) {
            if (currentFiles.contains(fileDeleted)) {
                currentFiles.remove(fileDeleted);
            }
        }
        AdfFacesContext.getCurrentInstance().addPartialTarget(iterVN);
    }

    public void deleteFileEN(ActionEvent actionEvent) {
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        fileIterator.setCurrentRowWithKeyValue(fileDeleted);
        fileIterator.removeCurrentRow();
        for (int i = 0; i < currentFiles.size(); i++) {
            if (currentFiles.contains(fileDeleted)) {
                currentFiles.remove(fileDeleted);
            }
        }
        AdfFacesContext.getCurrentInstance().addPartialTarget(iterEN);
    }

    public void searchVN() {
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
        ViewObject vo = baivietIterator.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("AptcBaivietView_VNCriteria");
        vo.ensureVariableManager().setVariableValue("varTieude",
                                                    this.tieudeTimkiemVN);
        vo.ensureVariableManager().setVariableValue("varSo",
                                                    soSelectedValueVN);
        vo.ensureVariableManager().setVariableValue("varChuyenmuc",
                                                    chuyenmucSelectedValueVN);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("AptcBaivietView_VNCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.executeQuery();
    }

    public void searchEN() {
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
        ViewObject vo = baivietIterator.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("AptcBaivietView_ENCriteria");
        vo.ensureVariableManager().setVariableValue("varTieude",
                                                    this.tieudeTimkiemEN);
        vo.ensureVariableManager().setVariableValue("varSo",
                                                    soSelectedValueEN);
        vo.ensureVariableManager().setVariableValue("varChuyenmuc",
                                                    chuyenmucSelectedValueEN);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("AptcBaivietView_ENCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.executeQuery();
    }

    public void rowSelectedVN(SelectionEvent se) {
        Utils.rowSelected(se, "AptcBaivietView_VN1");
    }

    public String goToPageVN() {
        Utils.goToPage(RANGE_SIZE, pageNumberVN, "AptcBaivietView_VN1");
        return null;
    }

    public String goLastPageVN() {
        Utils.goLastPage(RANGE_SIZE, "AptcBaivietView_VN1");
        return null;
    }

    public boolean isLastEnabledVN() {
        return Utils.isLastEnabled(RANGE_SIZE, "AptcBaivietView_VN1");
    }

    public boolean isFirstEnabledVN() {
        return Utils.isFirstEnabled(RANGE_SIZE, "AptcBaivietView_VN1");
    }

    private JUCtrlRangeBinding getBaivietViewVN() {
        return Utils.getViewObject("AptcBaivietView_VN1");
    }

    public void setPageNumberVN(int pageNumberVN) {
        JUCtrlRangeBinding baivietView = this.getBaivietViewVN();
        if (pageNumberVN >
            Math.max(1, (baivietView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumberVN =
                    (int)Math.max(1, (baivietView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumberVN = pageNumberVN;
    }

    public int getPageNumberVN() {
        return pageNumberVN =
                getBaivietViewVN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
    }

    //Paging for English

    public void rowSelectedEN(SelectionEvent se) {
        Utils.rowSelected(se, "AptcBaivietView_EN1");
    }

    public String goToPageEN() {
        Utils.goToPage(RANGE_SIZE, pageNumberEN, "AptcBaivietView_EN1");
        return null;
    }

    public String goLastPageEN() {
        Utils.goLastPage(RANGE_SIZE, "AptcBaivietView_EN1");
        return null;
    }

    public boolean isLastEnabledEN() {
        return Utils.isLastEnabled(RANGE_SIZE, "AptcBaivietView_EN1");
    }

    public boolean isFirstEnabledEN() {
        return Utils.isFirstEnabled(RANGE_SIZE, "AptcBaivietView_EN1");
    }

    private JUCtrlRangeBinding getBaivietViewEN() {
        return Utils.getViewObject("AptcBaivietView_EN1");
    }

    public void setPageNumberEN(int pageNumberEN) {
        JUCtrlRangeBinding baivietView = this.getBaivietViewEN();
        if (pageNumberEN >
            Math.max(1, (baivietView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumberEN =
                    (int)Math.max(1, (baivietView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumberEN = pageNumberEN;
    }

    public int getPageNumberEN() {
        return pageNumberEN =
                getBaivietViewEN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
    }

    RichTable tblVN = new RichTable();

    public void setTblVN(RichTable tblVN) {
        this.tblVN = tblVN;
    }

    public RichTable getTblVN() {
        return tblVN;
    }

    RichTable tblEN = new RichTable();

    public void setTblEN(RichTable tblEN) {
        this.tblEN = tblEN;
    }

    public RichTable getTblEN() {
        return tblEN;
    }

    public void setUnZipFile(boolean unZipFile) {
        this.unZipFile = unZipFile;
    }

    public boolean isUnZipFile() {
        return unZipFile;
    }

    public void setFileDeleted(String fileDeleted) {
        this.fileDeleted = fileDeleted;
    }

    public String getFileDeleted() {
        return fileDeleted;
    }

    public void setIterVN(UIComponent iterVN) {
        this.iterVN = iterVN;
    }

    public UIComponent getIterVN() {
        return iterVN;
    }

    public void setIterEN(UIComponent iterEN) {
        this.iterEN = iterEN;
    }

    public UIComponent getIterEN() {
        return iterEN;
    }

    public static void setHasFile(boolean hasFile) {
        baivietbean.hasFile = hasFile;
    }

    public static boolean isHasFile() {
        return hasFile;
    }

    public void setFileExisted(boolean fileExisted) {
        this.fileExisted = fileExisted;
    }

    public boolean isFileExisted() {
        if (operationType == 1 && hasFile == true) {
            fileExisted = true;
        }
        return fileExisted;
    }

    public static void setCurrentFiles(ArrayList<String> currentFiles) {
        baivietbean.currentFiles = currentFiles;
    }

    public static ArrayList<String> getCurrentFiles() {
        return currentFiles;
    }

    public void previewVn() {
        getHtFileObjectVn();
        Utils.showPopup(viewPopupVN);
    }

    public void previewEn() {
        getHtFileObjectEn();
        Utils.showPopup(viewPopupEN);
    }

    public void getHtFileObjectVn() {
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        ViewObject vo = fileIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "IN (" + getFiledinhkemVn() + ")";
        cr.setAttribute("Id", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
    }

    public void getHtFileObjectEn() {
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        ViewObject vo = fileIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "IN (" + getFiledinhkemEn() + ")";
        cr.setAttribute("Id", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
    }

    public void setViewPopupVN(RichPopup viewPopupVN) {
        this.viewPopupVN = viewPopupVN;
    }

    public RichPopup getViewPopupVN() {
        return viewPopupVN;
    }

    public void setViewPopupEN(RichPopup viewPopupEN) {
        this.viewPopupEN = viewPopupEN;
    }

    public RichPopup getViewPopupEN() {
        return viewPopupEN;
    }

    public void setFiledinhkemVn(String filedinhkemVn) {
        this.filedinhkemVn = filedinhkemVn;
    }

    public String getFiledinhkemVn() {
        DCIteratorBinding thutucIterator =
            Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
        Object td = thutucIterator.getCurrentRow().getAttribute("FileId");
        if (td == null)
            filedinhkemVn = "";
        else
            filedinhkemVn = td.toString();
        return filedinhkemVn;
    }

    public void setFiledinhkemEn(String filedinhkemEn) {
        this.filedinhkemEn = filedinhkemEn;
    }

    public String getFiledinhkemEn() {
        DCIteratorBinding thutucIterator =
            Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
        Object td = thutucIterator.getCurrentRow().getAttribute("FileId");
        if (td == null)
            filedinhkemEn = "";
        else
            filedinhkemEn = td.toString();
        return filedinhkemEn;
    }

    public void setTieudeTimkiemVN(String tieudeTimkiemVN) {
        this.tieudeTimkiemVN = tieudeTimkiemVN;
    }

    public String getTieudeTimkiemVN() {
        return tieudeTimkiemVN;
    }

    public void setTieudeTimkiemEN(String tieudeTimkiemEN) {
        this.tieudeTimkiemEN = tieudeTimkiemEN;
    }

    public String getTieudeTimkiemEN() {
        return tieudeTimkiemEN;
    }

    public void setTieudeVN(String tieudeVN) {
        this.tieudeVN = tieudeVN;
    }

    public String getTieudeVN() {
        if (operationType == 1) {
            DCIteratorBinding baivietIterator =
                Utils.getIteratorBinding("AptcBaivietView_VN1Iterator");
            if (baivietIterator.getAllRowsInRange().length > 0) {
                tieudeVN =
                        baivietIterator.getCurrentRow().getAttribute("Tieude").toString();
            }
        }
        return tieudeVN;
    }

    public void setTieudeEN(String tieudeEN) {
        this.tieudeEN = tieudeEN;
    }

    public String getTieudeEN() {
        if (operationType == 1) {
            DCIteratorBinding baivietIterator =
                Utils.getIteratorBinding("AptcBaivietView_EN1Iterator");
            if (baivietIterator.getAllRowsInRange().length > 0) {
                tieudeEN =
                        baivietIterator.getCurrentRow().getAttribute("Tieude").toString();
            }
        }
        return tieudeEN;
    }

    public void setNewPopupDeleteVN(RichPopup newPopupDeleteVN) {
        this.newPopupDeleteVN = newPopupDeleteVN;
    }

    public RichPopup getNewPopupDeleteVN() {
        return newPopupDeleteVN;
    }

    public void setNewPopupDeleteEN(RichPopup newPopupDeleteEN) {
        this.newPopupDeleteEN = newPopupDeleteEN;
    }

    public RichPopup getNewPopupDeleteEN() {
        return newPopupDeleteEN;
    }

    public void openDeletePopupVN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("AptcBaivietView_VN1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Bạn chưa chọn bài viết nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            Utils.showPopup(newPopupDeleteVN);
        }
    }

    public void openDeletePopupEN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("AptcBaivietView_EN1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Bạn chưa chọn bài viết nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            Utils.showPopup(newPopupDeleteEN);
        }
    }

    public void setSoSelectedValueVN(String soSelectedValueVN) {
        this.soSelectedValueVN = soSelectedValueVN;
    }

    public String getSoSelectedValueVN() {
        return soSelectedValueVN;
    }

    public void setSoElementsVN(SelectItem[] soElementsVN) {
        this.soElementsVN = soElementsVN;
    }

    public SelectItem[] getSoElementsVN() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoVNAccessor1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        soElementsVN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Sotapchi").toString());
            }
            soElementsVN[i] = item;
        }
        return soElementsVN;
    }

    public void setChuyenmucSelectedValueVN(String chuyenmucSelectedValueVN) {
        this.chuyenmucSelectedValueVN = chuyenmucSelectedValueVN;
    }

    public String getChuyenmucSelectedValueVN() {
        return chuyenmucSelectedValueVN;
    }

    public void setChuyenmucElementsVN(SelectItem[] chuyenmucElementsVN) {
        this.chuyenmucElementsVN = chuyenmucElementsVN;
    }

    public SelectItem[] getChuyenmucElementsVN() {
        if (soSelectedValueVN != null) {
            getChuyenmucVN(soSelectedValueVN);
        }
        return chuyenmucElementsVN;
    }

    public void setSoSelectedValueEN(String soSelectedValueEN) {
        this.soSelectedValueEN = soSelectedValueEN;
    }

    public String getSoSelectedValueEN() {
        return soSelectedValueEN;
    }

    public void setSoElementsEN(SelectItem[] soElementsEN) {
        this.soElementsEN = soElementsEN;
    }

    public SelectItem[] getSoElementsEN() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoENAccessor1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        soElementsEN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Sotapchi").toString());
            }
            soElementsEN[i] = item;
        }
        return soElementsEN;
    }

    public void setChuyenmucSelectedValueEN(String chuyenmucSelectedValueEN) {
        this.chuyenmucSelectedValueEN = chuyenmucSelectedValueEN;
    }

    public String getChuyenmucSelectedValueEN() {
        return chuyenmucSelectedValueEN;
    }

    public void setChuyenmucElementsEN(SelectItem[] chuyenmucElementsEN) {
        this.chuyenmucElementsEN = chuyenmucElementsEN;
    }

    public SelectItem[] getChuyenmucElementsEN() {
        if (soSelectedValueEN != null) {
            getChuyenmucEN(soSelectedValueEN);
        }
        return chuyenmucElementsEN;
    }

    public void setInputFileVN(RichInputFile inputFileVN) {
        this.inputFileVN = inputFileVN;
    }

    public RichInputFile getInputFileVN() {
        return inputFileVN;
    }

    public void setInputFileEN(RichInputFile inputFileEN) {
        this.inputFileEN = inputFileEN;
    }

    public RichInputFile getInputFileEN() {
        return inputFileEN;
    }

    public void setSoSelectChoiceVN(RichSelectOneChoice soSelectChoiceVN) {
        this.soSelectChoiceVN = soSelectChoiceVN;
    }

    public RichSelectOneChoice getSoSelectChoiceVN() {
        return soSelectChoiceVN;
    }

    public void setSoSelectChoiceEN(RichSelectOneChoice soSelectChoiceEN) {
        this.soSelectChoiceEN = soSelectChoiceEN;
    }

    public RichSelectOneChoice getSoSelectChoiceEN() {
        return soSelectChoiceEN;
    }

    public void setChuyenmucSelectChoiceVN(RichSelectOneChoice chuyenmucSelectChoiceVN) {
        this.chuyenmucSelectChoiceVN = chuyenmucSelectChoiceVN;
    }

    public RichSelectOneChoice getChuyenmucSelectChoiceVN() {
        return chuyenmucSelectChoiceVN;
    }

    public void setChuyenmucSelectChoiceEN(RichSelectOneChoice chuyenmucSelectChoiceEN) {
        this.chuyenmucSelectChoiceEN = chuyenmucSelectChoiceEN;
    }

    public RichSelectOneChoice getChuyenmucSelectChoiceEN() {
        return chuyenmucSelectChoiceEN;
    }

    public void setButtonDownload(RichCommandButton buttonDownload) {
        this.buttonDownload = buttonDownload;
    }

    public RichCommandButton getButtonDownload() {
        return buttonDownload;
    }

    public void downloadfile(ActionEvent actionEvent) {
        String fileName =
            (String)actionEvent.getComponent().getAttributes().get("fileName");
        buttonDownload.setText(fileName);
        FacesContext context = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks =
            Service.getService(context.getRenderKit(),
                               ExtendedRenderKitService.class);
        erks.addScript(context, "customHandler();");
    }

    public void dwdFile(FacesContext facesContext, OutputStream out) {
        File duongdan =
            new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                     Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"));
        File file = new File(duongdan + "/" + buttonDownload.getText());
        FileInputStream Fileinput = null;
        byte[] bt;
        try {
            Fileinput = new FileInputStream(file);
            int checkline;
            while ((checkline = Fileinput.available()) > 0) {
                bt = new byte[checkline];
                int rst = Fileinput.read(bt);
                out.write(bt, 0, bt.length);
                if (rst == -1)
                    break;

            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
